package com.bbm.communitysystem.core.redis;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class FileRedis {

    private final Redis redis;

    private final String FILE_KEY = "BBM_FILE_KEY:";

    public void fileChunkCreate (String hash) {
        redis.setValue(FILE_KEY + hash, 0);
    }

    public int fileChunkAdd (String hash) {
        Object value = redis.getValue(FILE_KEY + hash);
        if (value == null) {
            return 1;
        }

        redis.setValue(FILE_KEY + hash, Integer.parseInt(value + "") + 1);

        return 0;
    }

    public void removeFileChunk (String hash) {
        redis.deleteValue(FILE_KEY + hash);
    }

    public Integer getFileChunk (String hash) {
        Object value = redis.getValue(FILE_KEY + hash);
        if (value == null) {
            return null;
        }
        return Integer.parseInt(value + "");
    }

}
